package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Aviso;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.service.AvisoService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Carlos
 */
@Controller("avisoController")
public class AvisoController {

    @Autowired
    private AvisoService avisoService;
    private static Logger log = LoggerFactory.getLogger(AvisoController.class);

    @RequestMapping("/avisos/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("avisos/index");
        List<Aviso> avisos = avisoService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("avisos", avisos);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
    
    @RequestMapping(value = "/avisos/new", method = RequestMethod.GET)
    public ModelAndView newAviso() {
        ModelAndView mav = new ModelAndView("avisos/new");
        Aviso aviso = new Aviso();
        mav.getModelMap().put("aviso", aviso);
        return mav;
    }

    @RequestMapping(value = "/avisos/new", method = RequestMethod.POST)
    public String createAviso(@ModelAttribute("aviso")Aviso aviso, SessionStatus status) {    
        avisoService.insertar(aviso);
        status.setComplete();
        return "redirect:/pages/avisos/index";
    }
    
    @RequestMapping(value = "/avisos/edit", method = RequestMethod.GET)
    public ModelAndView editAviso(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("avisos/edit");
        Aviso aviso = avisoService.buscar(id);
        mav.getModelMap().put("aviso", aviso);
        return mav;        
    }
    
    @RequestMapping(value="/avisos/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("aviso") Aviso aviso, SessionStatus status) {
        avisoService.actualizar(aviso);
        status.setComplete();
        return "redirect:/pages/avisos/index";
    }
    
    @RequestMapping("/avisos/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/avisos/index");
        Aviso aviso = avisoService.buscar(id);
        avisoService.eliminar(aviso);
        return mav;
    }
    
    @RequestMapping(value = "/avisos/buscar", method=RequestMethod.POST)
    public ModelAndView buscar(@ModelAttribute("criterioBusqueda") CriterioBusqueda criterio, SessionStatus status) {
        ModelAndView mav = new ModelAndView("avisos/resultadoBusqueda");
        log.info("resultado = " + avisoService.buscarPorTitulo(criterio.getTitulo()).size());
        mav.getModel().put("resultado",avisoService.buscarPorTitulo(criterio.getTitulo()));
        return mav;
    }
    
}
